package com.mcptt.db.model;

import java.io.Serializable;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.DatabaseUtils;

import com.cloudptt.api.comm.CheckData;
import com.cloudptt.api.product.log.Log;
import com.mcptt.comm.CommonFunc;
import com.mcptt.db.DbConst;

public class SessionInfo implements Serializable 
{
	public static final int SessionType_Single = 1;
	public static final int SessionType_Group = 2;
	public static final int SessionType_Sys = 3;

	public final static int Msgtype_Txt = 0;
	//	public final static int Msgtype_Pic = 1;
	public final static int Msgtype_File = 2;

	public final static int Filetype_File = 1;
	public final static int Filetype_Pic = 2;
	public final static int Filetype_Voice = 3;
	public final static int Filetype_Video = 4;

	public final static int Msgtype_Urgency = 6;
	public final static int Msgtype_CallLog = 8;
	//	public final static int Msgtype_CallLog_Incomplete = 9;
	public final static int Msgtype_Affiliation  = 10;

	public final static int Direction_Type_Send = 1;
	public final static int Direction_Type_Receive = 2;

	public final static int Status_Read = 1;
	public final static int Status_Unread = 2;
	public final static int Status_SendFailed = 3;


	private AddrInfo receiver = null;  //接受方属性
	private AddrInfo sender = null;  //发送方属性

	private int msgType = Msgtype_Txt; 		 	//消息类型(图片、文字)
	private String content="";      		//消息内容
	private String date="";				//发送时间
	private int direction = Direction_Type_Send;    			//收发标记（1：发  2：收）
	private long len = 0;
	private int status = Status_Unread;  			//消息状态（1：已读 2：发送失败 3：未读 4：未接听 5：已接听 6：主呼）

	/**
	 * 
	 * 及时消息
	 * @param MDN      会话对方MDN（群组消息时填0）
	 * @param MsgType  消息类型(图片、文字)
	 * @param modeType 消息模式URL  (1:个人 2：群组)
	 * @param msg      消息内容
	 * @param date     发送时间
	 * @param flag     收发标记（1：发  2：收）
	 * @param name     用户名称（会话对方的名称）
	 * @param guid     群组ID
	 * @param gname    群组名称
	 * @param status   消息状态（1：已读 2：发送失败 3：未读 4：未接听 5：已接听 6：主呼）
	 */
	public SessionInfo(AddrInfo receiver,String senderNum,int msgType,String content,long len)
	{
		this.receiver = receiver;
		this.sender = new AddrInfo();
		this.sender.setType(UserInfo.UserType_User);
		//		this.sender.setuId(senderUId);
		this.sender.setMcId(senderNum);
		this.sender.setName(senderNum);
		this.msgType = msgType;
		this.content = content;
		this.len = len;
		this.date = CommonFunc.getDateAndTime();
		this.direction = Direction_Type_Send;
		this.status = Status_Unread;
	}

	public SessionInfo(AddrInfo receiver,AddrInfo sender,int msgType,String content,long len)
	{
		this.receiver = receiver;
		this.sender = sender;
		this.msgType = msgType;
		this.content = content;
		this.len = len;
		this.date = CommonFunc.getDateAndTime();
		this.direction = Direction_Type_Receive;
		this.status = Status_Unread;
	}

	public SessionInfo(AddrInfo receiver,AddrInfo sender,int msgType,int direction,String content,long len)
	{
		this.receiver = receiver;
		this.sender = sender;
		this.msgType = msgType;
		this.direction = direction;
		this.content = content;
		this.len = len;
		this.date = CommonFunc.getDateAndTime();
		this.status = Status_Unread;
	}


	public SessionInfo(Cursor c) 
	{
		this.receiver = new AddrInfo();
		this.receiver.setType(UserInfo.UserType_User);

		this.sender = new AddrInfo();
		this.sender.setType(UserInfo.UserType_User);

		ContentValues args = new ContentValues();
		DatabaseUtils.cursorRowToContentValues(c, args);
		createFromContentValue(args);
	}
	public final void createFromContentValue(ContentValues args) 
	{
		String tmp_s;
		//		tmp_s = args.getAsString(DbConst.COL_ReceiverUId);
		//		if(tmp_s != null) {
		//			receiver.setuId(tmp_s);
		//		}
		tmp_s = args.getAsString(DbConst.COL_ReceiverNum);
		if(tmp_s != null) {
			receiver.setMcId(tmp_s);
		}
		//		tmp_s = args.getAsString(DbConst.COL_SenderUId);
		//		if(tmp_s != null) {
		//			sender.setuId(tmp_s);
		//		}
		tmp_s = args.getAsString(DbConst.COL_SenderId);
		if(tmp_s != null) {
			sender.setMcId(tmp_s);
		}
		tmp_s = args.getAsString(DbConst.COL_Content);
		if(tmp_s != null) {
			content = tmp_s;
		}
		tmp_s = args.getAsString(DbConst.COL_Date);
		if(tmp_s != null) {
			date = tmp_s;
		}
		tmp_s = args.getAsString(DbConst.COL_Direction);
		if(tmp_s != null) {
			direction = CheckData.parseInt(tmp_s);
		}
		tmp_s = args.getAsString(DbConst.COL_Len);
		if(tmp_s != null) {
			len = CheckData.parseLong(tmp_s);
		}
		tmp_s = args.getAsString(DbConst.COL_MsgType);
		Log.debug(getClass(), "createFromContentValue tmp_s = " + tmp_s);
		if(tmp_s != null) {
			msgType = CheckData.parseInt(tmp_s);
			Log.debug(getClass(), "createFromContentValue msgType = " + msgType);
		}
		tmp_s = args.getAsString(DbConst.COL_Status);
		if(tmp_s != null) {
			status = CheckData.parseInt(tmp_s);
		}

		tmp_s = args.getAsString(DbConst.COL_Type);
		if(tmp_s != null) 
		{
			int value = CheckData.parseInt(tmp_s);
			if(value != -1)
			{
				sender.setType(value);
			}
		}
		tmp_s = args.getAsString(DbConst.COL_Name);
		if(tmp_s != null) {
			sender.setName(tmp_s);
		}
	}

	/**
	 * Pack the object content value to store
	 * 
	 * @return The content value representing the message
	 */
	public ContentValues getContentValues() {

		ContentValues cv = new ContentValues();
		//		cv.put(DbConst.COL_ReceiverUId, receiver.getuId());
		cv.put(DbConst.COL_ReceiverNum, receiver.getMcId());
		//		cv.put(DbConst.COL_SenderUId, sender.getuId());
		cv.put(DbConst.COL_SenderId, sender.getMcId());
		cv.put(DbConst.COL_Content, content);
		cv.put(DbConst.COL_Date, date);
		cv.put(DbConst.COL_Direction, direction);
		cv.put(DbConst.COL_Len, len);
		cv.put(DbConst.COL_MsgType, msgType);
		Log.debug(getClass(), "getContentValues msgType = " + msgType);
		cv.put(DbConst.COL_Status, status);
		return cv;
	}

	public HistoryInfo toHistoryInfo()
	{
		HistoryInfo info = new HistoryInfo(receiver);
		info.setLastSender(sender);
		info.setLastMsgContent(content);
		info.setLastMsgDate(date);
		info.setLastMsgType(msgType);

		return info;
	}

	public AddrInfo getSender() {
		return sender;
	}

	public void setReceiver(UserInfo receiver) {
		this.receiver = receiver;
	}
	public AddrInfo getReceiver() {
		return receiver;
	}

	public int getMsgtype() {
		return msgType;
	}

	public void setMsgtype(int msgtype) {
		this.msgType = msgtype;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}
	public int getDirection() {
		return direction;
	}
	public void setDirection(int direction) {
		this.direction = direction;
	}
	public long getLen() {
		return len;
	}
	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public String getDscp()
	{
		return sender.getDscp();
	}

	public String toString()
	{
		StringBuffer buffer = new StringBuffer("SessionInfo:");
		buffer.append("receiver = ").append(receiver).append(",");
		buffer.append("sender = ").append(sender).append(",");
		buffer.append("content = ").append(content).append(",");
		buffer.append("msgtype = ").append(msgType).append(",");
		buffer.append("date = ").append(date).append(",");
		buffer.append("direction = ").append(direction).append(",");
		buffer.append("len = ").append(len).append(",");
		buffer.append("status = ").append(status).append(";");

		return buffer.toString();
	}
}
